<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="itemQuery.name" placeholder="商品名称" />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getPageList()"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <el-table :data="itemList" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商品id">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="商品标题">
              <span>{{ props.row.title }}</span>
            </el-form-item>
            <el-form-item label="商品名称">
              <span>{{ props.row.name }}</span>
            </el-form-item>
            <el-form-item label="价格">
              <span>{{ props.row.price }}</span>
            </el-form-item>
            <el-form-item label="一级分类">
              <span>{{ props.row.oneId }}</span>
            </el-form-item>
            <el-form-item label="二级分类">
              <span>{{ props.row.twoId }}</span>
            </el-form-item>
            <el-form-item label="三级分类">
              <span>{{ props.row.threeId }}</span>
            </el-form-item>
            <el-form-item label="品牌">
              <span>{{ props.row.brandId }}</span>
            </el-form-item>
            <el-form-item label="状态">
              <span>{{ props.row.status }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="商品 ID" prop="id"></el-table-column>
      <el-table-column label="商品名称" prop="name"></el-table-column>
      <el-table-column label="价格" prop="price"></el-table-column>
      <el-table-column label="品牌" prop="brandId"></el-table-column>
      <el-table-column label="一级分类" prop="oneId"></el-table-column>
      <el-table-column label="二级分类" prop="twoId"></el-table-column>
      <el-table-column label="三级分类" prop="threeId"></el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <!--  <router-link :to="'/item/edit/'+scope.row.id">-->
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="updateItem(scope.row)"
            >修改</el-button
          >
          <!--  </router-link> -->
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="prev, pager, next"
      :current-page="page"
      :page-size="limit"
      :total="total"
      @current-change="getPageList"
    ></el-pagination>
  </div>
</template>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>

<script>
import { fetchList, getObj, addObj, putObj, delObj } from "@/api/info";
export default {
  data() {
    return {
      itemBo: {
        name: "",
        title: "",
        price: 0,
        status: "",
        oneId: "",
        twoId: "",
        threeId: "",
        brandId: "",
      },
      itemList: null,
      page: 1, //当前页
      limit: 5, //每页记录数
      total: 0, //总记录数
      itemQuery: {},
    };
  },
  created() {
    this.getPageList();
  },
  methods: {
    //分页查询
    getPageList(page = 1) {
      this.page = page;
      fetchList(this.page, this.limit, this.itemQuery).then((response) => {
        this.itemList = response.data.data.records;
        this.total = response.data.data.total;
      });
    },
    //根据id删除商品信息
    removeDataById(id) {
      this.$confirm("此操作将永久删除该商品信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delObj(id).then((response) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getPageList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    updateItem(id) {
      //路径跳转，并根据router来传数据
      this.$router.push({ path: "/pig-item/item/update", query: { id: id } });
    },
    resetData() {
      this.itemQuery.name = "";
    },
  },
};
</script>